package com.atguigu.lease.admin.controller.system;

import com.atguigu.lease.admin.service.LoginService;
import com.atguigu.lease.common.handler.StgyAdminAuthInterceptor;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.SystemUser;
import com.atguigu.lease.model.vo.system.CaptchaVo;
import com.atguigu.lease.model.vo.system.LoginVo;
import io.jsonwebtoken.impl.DefaultClaims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author LOGIN
 * @title LoginController
 * @create 2025/6/28 10:19
 * @description
 */
@Tag(name="后台用户登陆管理模块")
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Operation(summary = "3、登陆成功查询用户信息")
    @GetMapping("/info")
    public Result info(@RequestHeader("ACCESS_TOKEN")String token) {
        //1、获取请求头中的token：
        //2、使用jwt工具类解析token：  可以判断token是否失效 或者 被篡改
        /*DefaultClaims claims = JwtUtil.parseToken(token);
        String avatarUrl = claims.get("avatarUrl", String.class);
        String name = claims.get("name", String.class);*/
        //获取拦截器透传用户对象
        SystemUser systemUser = StgyAdminAuthInterceptor.SYSTEM_USER_THREAD_LOCAL.get();
        //3、解析成功：
        //  按照前端的要求响应数据
        Map<String,Object> map = new HashMap<>();
        map.put("name",systemUser.getName());
        map.put("avatarUrl",systemUser.getAvatarUrl());
        return Result.ok(map);
    }

    @Operation(summary = "2、登录功能")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @Operation(summary = "1、获取验证码")
    @GetMapping("/login/captcha")
    public Result<CaptchaVo> captcha(){
        CaptchaVo captchaVo = loginService.getCaptcha();
        return Result.ok(captchaVo);
    }
}
